<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')"
             :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"
             @keyup.enter.native="dataFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="房间号" prop="roomNumber">
        <el-input v-model="dataForm.roomNumber" placeholder="房间号"></el-input>
      </el-form-item>
      <el-form-item label="房间价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="房间价格"></el-input>
      </el-form-item>
<!--      <el-form-item label="特惠价格" prop="discountsPrice">-->
<!--        <el-input v-model="dataForm.discountsPrice" placeholder="特惠价格"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="房间类型" prop="dictId">
        <el-select v-model="dataForm.dictId" clearable placeholder="请选择">
          <el-option
              v-for="item in dictIds"
              :key="item.id"
              :label="item.dictLabel"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="简介" prop="briefIntroduction">
        <el-input v-model="dataForm.briefIntroduction" placeholder="简介"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="dataForm.state" placeholder="请选择">
          <el-option
              v-for="item in states"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="所属民宿" prop="homeId">-->
<!--        <el-select v-model="dataForm.homeId" clearable placeholder="请选择">-->
<!--          <el-option-->
<!--              v-for="item in homeIds"-->
<!--              :key="item.id"-->
<!--              :label="item.location"-->
<!--              :value="item.id">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      visible1: false,
      dictIds: [],
      homeIds:[],
      states: [{
        value: '1',
        label: '启用'
      }, {
        value: '2',
        label: '禁用'
      }],
      dataForm: {
        id: '',
        roomNumber: '',
        price: '',
        discountsPrice: '',
        dictId: '',
        briefIntroduction: '',
        state: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        homeId: ''
      }
    }
  },
  computed: {
    dataRule() {
      return {
        roomNumber: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        price: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        discountsPrice: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        dictId: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        briefIntroduction: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        state: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        homeId: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    }
  }, mounted() {
    this.getDictId();
    this.searchAll();

  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/biz/roominformation/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/biz/roominformation/', this.dataForm).then(
            ({data: res}) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.$message({
                message: this.$t('prompt.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false}),
    getDictId: function () {
      //位置
      let dictTypeId = "1623227835625279489";
      this.$http.get(`/sys/dict/data/getDataByPid?dictTypeId=` + dictTypeId).then(
          ({data: res}) => {
            this.dictIds = res.data
          })
    },
    searchAll: function () {
      this.$http.get(`/biz/homestayinformation/searchAll`).then(
          ({data: res}) => {
            this.homeIds = res.data
          })
    }
  }
}
</script>
